/**
 * Copyright (C) 2010, 2011 Neofonie GmbH
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.dicodeproject.analysis.restapi.controllers;

import twitter4j.internal.org.json.JSONException;
import twitter4j.internal.org.json.JSONObject;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author jakob
 */
public class MetaDataController extends ControllerBase {

  /**
   * Generic method which returns Twitter meta-data from HBase
   * http://localhost:9889/restapi/twitter/metadata/lang?day=20110824
   * @param source source table (e.g. twitter or blogs)
   * @param type type of metadata; directly used in row id
   * @param day date for which to get the metadata
   * @param callback JSON callback string
   * @param response object to write the result to
   * @throws java.io.IOException inherited from HBase Scan and HttpServletResponse.write
   * @throws twitter4j.internal.org.json.JSONException inherited from JSONObject constructor
   */
  // two versions (.json and without) for backward-compatibility: TODO adapt dicode-demo
  @RequestMapping(value = {"{source}/metadata/{type}.json", "{source}/metadata/{type}"}, method = RequestMethod.GET)
  public void metadata(@PathVariable String source,
      @PathVariable String type, @RequestParam String day, @RequestParam(required = false) String callback,
      HttpServletResponse response) throws IOException, JSONException {

    // parameters
    String tableName = getTableName(source);

    HTableInterface hTable = getHTable(tableName);
    Get get = new Get(Bytes.toBytes(type + "_" + day));
    get.addColumn(this.columnFamily, this.columnQualifier);
    Result result = hTable.get(get);

    JSONObject json = new JSONObject();
    if (!result.isEmpty()) {
      String jsonString = Bytes.toString(result.value());  // metadata are in HBase as JSON strings
      json = new JSONObject(jsonString);
    }

    int numResults;
    try {
      numResults = json.getJSONArray("result").length();
    } catch (JSONException e) {
      numResults = 0;
    }

    JSONObject params = new JSONObject();
    params.put("type", type);
    params.put("day", day);
    params.put("results", numResults);
    json.put("parameters", params);

    respond(callback, json, response);
  }

}
